package com.mbyte.easy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.admin.entity.*;
import com.mbyte.easy.admin.mapper.AllotMapper;
import com.mbyte.easy.admin.mapper.DraftMapper;
import com.mbyte.easy.admin.mapper.GroupMapper;
import com.mbyte.easy.admin.mapper.RelationMapper;
import com.mbyte.easy.admin.service.*;
import com.mbyte.easy.common.constant.*;
import com.mbyte.easy.event.CmsEventPublisher;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.security.mapper.SysUserRolesMapper;
import com.mbyte.easy.support.es.constants.EsConstants;
import com.mbyte.easy.support.es.helper.EsDraftHelper;
import com.mbyte.easy.util.DateUtil;
import com.mbyte.easy.util.Utility;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2019-12-12
 */
@Slf4j
@Transactional
@Service
public class DraftServiceImpl extends ServiceImpl<DraftMapper, Draft> implements IDraftService {

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRolesMapper sysUserRolesMapper;
    @Autowired
    private AllotMapper allotMapper;
    @Autowired
    private RelationMapper relationMapper;
    @Autowired
    DraftMapper draftMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    ICheckPartnerService checkPartnerService;
    @Autowired
    IRelationService relationService;
    @Autowired
    IDraftService draftService;
    @Autowired
    IPptService pptService;
    @Autowired
    IVideoService videoService;
    @Autowired
    IAudioService audioService;
    @Autowired
    ICheckService checkService;
    @Autowired
    private CmsEventPublisher cmsEventPublisher;
    @Autowired
    private EsDraftHelper esDraftHelper;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;


    @Override
    public IPage<Draft> listPage(IPage<Draft> page, QueryWrapper<Draft> queryWrapper) {
        return page.setRecords(this.baseMapper.listPage(page, queryWrapper));
    }

    @Override
    public IPage<Draft> pageList(IPage<Draft> page, QueryWrapper<Draft> queryWrapper) {
        return page.setRecords(this.baseMapper.pageList(page, queryWrapper));
    }

    @Override
    public void insert2Id(Draft draft) {
        draftMapper.insert2Id(draft);
        cmsEventPublisher.publishChangeDraftEvent(draft
                .setEsSyncType(EsConstants.ES_SYNC_TYPE_ADD));
    }

    /**
     * 稿件查重
     * @param text
     * @return
     */
    @Override
    public List<Draft> queryByText(String text){
        return esDraftHelper.queryByText(text);
    }

    /**
     * @Description 写手新建文章
     * @param draft
     * @Author myr
     * @return
     **/
    @Override
    public boolean newDraft(Draft draft, String action) {
        draft.setSubmitTime(LocalDateTime.now());
        draft.setCreateTime(LocalDateTime.now());
        draft.setIsPass(DraftIsPass.NotCheck);
        draft.setUpdateTime(LocalDateTime.now());
        draft.setUserId(checkPartnerService.getCurrentUser().getId());
        draft.setIsDelete(DraftIsPass.NotDelete);
        draft.setLastOperator(checkPartnerService.getCurrentUser().getId());
        if(action.equals("save")){
            draft.setStatusCode(StateDic.Save);
            insert2Id(draft);
            //操作日志
            applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.SAVE)
                    .setDraftId(draft.getId()));
        }else if(action.equals("submit")){
            //判断是否有互查伙伴
            QueryWrapper<CheckPartner> checkPartnerQueryWrapper = new QueryWrapper<>();
            checkPartnerQueryWrapper.eq("user_id",draftService.getUserIdByUserName(Utility.getCurrentUsername()).getId());
            checkPartnerQueryWrapper.isNull("partner_id");
            List<CheckPartner> list = checkPartnerService.list(checkPartnerQueryWrapper);
            log.info("list-->{}",list);
            if(list.size() != 0){
                draft.setStatusCode(StateDic.Partner_Pass);
            }else{
                draft.setStatusCode(StateDic.Submit);
            }
            insert2Id(draft);
            //操作日志
            applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_ADD)
                    .setDraftId(draft.getId()));
        }
        return true;
    }

    @Override
    public boolean update(Draft draft){
        draft.setUpdateTime(LocalDateTime.now());
        boolean result = updateById(draft);
        if(result){
            cmsEventPublisher.publishChangeDraftEvent(draft
                    .setEsSyncType(EsConstants.ES_SYNC_TYPE_UPDATE));
        }
        return result;
    }
    @Override
    public boolean deleteByIds(List<Long> idList){
        boolean result = removeByIds(idList);
        if(result){
            cmsEventPublisher.publishChangeDraftEvent(new Draft()
                    .setEsSyncType(EsConstants.ES_SYNC_TYPE_BATCH_DEL)
                    .setIdList(idList));
        }
        return result;
    }
    /**
     * @Description 写手用户的稿件列表
     * @Author myr
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage index(boolean isCheck,boolean isBox, Integer pageNo, Integer pageSize,Draft draft,String createTimeSpace,Boolean box) {
        Page<Draft> page = new Page<Draft>(pageNo, pageSize);
        QueryWrapper<Draft> queryWrapper = new QueryWrapper<Draft>();
        if (isCheck){
            QueryWrapper<CheckPartner> checkPartnerQueryWrapper = new QueryWrapper<>();
            checkPartnerQueryWrapper.eq("partner_id",userMapper.selectByUsername(Utility.getCurrentUsername()).getId());
            List<CheckPartner> checkPartner = checkPartnerService.list(checkPartnerQueryWrapper);
            if (!checkPartner.isEmpty()){
                Long userId = checkPartner.get(0).getUserId();
                queryWrapper.eq("user_id", userId);
            }
        }else{
            String loginUserName = Utility.getCurrentUsername();
            SysUser loginUser = userMapper.selectByUsername(loginUserName);
            Long userId = loginUser.getId();
            queryWrapper.eq("user_id", userId);
        }

        if (!ObjectUtils.isEmpty(createTimeSpace)){
            queryWrapper.lambda().between(Draft::getCreateTime, DateUtil.getStart(createTimeSpace)
                    ,DateUtil.getEnd(createTimeSpace).plusDays(1));
        }
        if(!ObjectUtils.isEmpty(draft.getSubmitTime())) {
            queryWrapper = queryWrapper.like("submit_time",draft.getSubmitTime());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())) {
            queryWrapper = queryWrapper.like("draft_title",draft.getDraftTitle());
        }

        if(!ObjectUtils.isEmpty(draft.getDraftType()) && draft.getDraftType() >= DraftType.Image_Text) {
            queryWrapper = queryWrapper.like("draft_type",draft.getDraftType());
        }
        if(box){
            if(!ObjectUtils.isEmpty(draft.getIsPass())) {
                queryWrapper = queryWrapper.eq("is_pass", draft.getIsPass());
            }
        }else{
            queryWrapper = queryWrapper.eq("is_pass",DraftIsPass.NotCheck);
        }
//        queryWrapper.lambda().orderByDesc(Draft::getCreateTime);
        if(isBox){
            queryWrapper.eq("status_code",StateDic.Save);
        }else{
            queryWrapper.eq("status_code",draft.getStatusCode());
        }
        if(draftService.selectRoleId() == SysRole.PARTTIME_SEVEN){
            queryWrapper.in("draft_type",DraftType.SHORT, DraftType.MID, DraftType.LENGHT,DraftType.ACCOUNTS);
        }
        queryWrapper.lambda().orderByDesc(Draft::getUpdateTime);
        IPage pageinfo = draftMapper.selectPage(page, queryWrapper);
        return pageinfo;
    }


    @Override
    public IPage indexVideo(boolean isCheck,boolean isBox, Integer pageNo, Integer pageSize,Draft draft,String createTimeSpace,Boolean box) {
        Page<Draft> page = new Page<Draft>(pageNo, pageSize);
        QueryWrapper<Draft> queryWrapper = new QueryWrapper<Draft>();
        if (isCheck){
            QueryWrapper<CheckPartner> checkPartnerQueryWrapper = new QueryWrapper<>();
            checkPartnerQueryWrapper.eq("partner_id",userMapper.selectByUsername(Utility.getCurrentUsername()).getId());
            List<CheckPartner> checkPartner = checkPartnerService.list(checkPartnerQueryWrapper);
            if (!checkPartner.isEmpty()){
                Long userId = checkPartner.get(0).getUserId();
                queryWrapper.eq("user_id", userId);
            }
        }else{
            String loginUserName = Utility.getCurrentUsername();
            SysUser loginUser = userMapper.selectByUsername(loginUserName);
            Long userId = loginUser.getId();
            queryWrapper.eq("user_id", userId);
        }

        if (!ObjectUtils.isEmpty(createTimeSpace)){
            queryWrapper.lambda().between(Draft::getCreateTime, DateUtil.getStart(createTimeSpace)
                    ,DateUtil.getEnd(createTimeSpace).plusDays(1));
        }
        if(!ObjectUtils.isEmpty(draft.getSubmitTime())) {
            queryWrapper = queryWrapper.like("submit_time",draft.getSubmitTime());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())) {
            queryWrapper = queryWrapper.like("draft_title",draft.getDraftTitle());
        }

        if(!ObjectUtils.isEmpty(draft.getDraftType()) && draft.getDraftType() >= DraftType.Image_Text) {
            queryWrapper = queryWrapper.like("draft_type",draft.getDraftType());
        }
        if(box){
            if(!ObjectUtils.isEmpty(draft.getIsPass())) {
                queryWrapper = queryWrapper.eq("is_pass", draft.getIsPass());
            }
        }else{
            queryWrapper = queryWrapper.eq("is_pass",DraftIsPass.NotCheck);
        }
        queryWrapper.lambda().orderByDesc(Draft::getCreateTime);
        if(isBox){
            queryWrapper.eq("status_code",StateDic.Save);
        }else{
            queryWrapper.eq("status_code",draft.getStatusCode());
        }
        if(draftService.selectRoleId() == SysRole.PARTTIME_SEVEN){
            queryWrapper.eq("draft_type",DraftType.SVIDEO);
        }
        IPage pageinfo = draftMapper.selectPage(page, queryWrapper);
        return pageinfo;
    }

    /**
     * @Description 查询需要检查的伙伴的稿件列表
     * @Author myr
     * @param pageNo
     * @param pageSize
     * @param draft
     * @return
     */
    @Override
    public IPage partnerIndex(Integer pageNo, Integer pageSize, Draft draft,String createTimeSpace) {
        SysUser currentUser = checkPartnerService.getCurrentUser();
        QueryWrapper<CheckPartner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",currentUser.getId());
        //获取伙伴的信息
        CheckPartner checkPartner = checkPartnerService.getOne(queryWrapper);
        if(checkPartner == null){
            return  new Page<>(pageNo,pageSize);
        }
        QueryWrapper<Draft> draftList =  new QueryWrapper<Draft>().eq("user_id",checkPartner.getPartnerId())
                .eq("status_code", draft.getStatusCode());
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            draftList.lambda().between(Draft::getCreateTime,begin,end);
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())) {
            draftList = draftList.like("draft_title",draft.getDraftTitle());
        }
        if(!ObjectUtils.isEmpty(draft.getSubmitTime())) {
            draftList = draftList.like("submit_time",draft.getSubmitTime());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftType()) && draft.getDraftType() >= DraftType.Image_Text) {
            draftList = draftList.like("draft_type",draft.getDraftType());
        }
        if(draftService.selectRoleId() == SysRole.PARTTIME_SEVEN){
            draftList.in("draft_type",DraftType.SHORT, DraftType.MID, DraftType.LENGHT,DraftType.ACCOUNTS);
        }
        draftList.lambda().orderByDesc(Draft::getCreateTime);
        IPage<Draft> iPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),draftList);

        return iPage;

    }


    @Override
    public IPage partnerVideoIndex(Integer pageNo, Integer pageSize, Draft draft,String createTimeSpace) {
        SysUser currentUser = checkPartnerService.getCurrentUser();
        QueryWrapper<CheckPartner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",currentUser.getId());
        //获取伙伴的信息
        CheckPartner checkPartner = checkPartnerService.getOne(queryWrapper);
        if(checkPartner == null){
            return  new Page<>(pageNo,pageSize);
        }
        QueryWrapper<Draft> draftList =  new QueryWrapper<Draft>().eq("user_id",checkPartner.getPartnerId())
                .eq("status_code", draft.getStatusCode());
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            draftList.lambda().between(Draft::getCreateTime,begin,end);
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())) {
            draftList = draftList.like("draft_title",draft.getDraftTitle());
        }
        if(!ObjectUtils.isEmpty(draft.getSubmitTime())) {
            draftList = draftList.like("submit_time",draft.getSubmitTime());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftType()) && draft.getDraftType() >= DraftType.Image_Text) {
            draftList = draftList.like("draft_type",draft.getDraftType());
        }
        if(draftService.selectRoleId() == SysRole.PARTTIME_SEVEN){
            draftList.eq("draft_type",DraftType.SVIDEO);
        }
        draftList.lambda().orderByDesc(Draft::getCreateTime);
        IPage<Draft> iPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),draftList);

        return iPage;

    }

    /**
     * @Description 版块负责人查询需要检查的下属稿件列表
     * @Author myr
     * @param pageNo
     * @param pageSize
     * @param draft
     * @return
     */
    @Override
    public IPage blockIndex(Integer pageNo, Integer pageSize, Draft draft,String createTimeSpace) {
        SysUser currentUser = checkPartnerService.getCurrentUser();
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",currentUser.getId());
        List<Long> collect = relationMapper.selectList(queryWrapper).stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
        QueryWrapper<Draft> qw = new QueryWrapper<>();
        collect.add(-1L);
//        if(!ObjectUtils.isEmpty(collect)) {
        if(selectRoleId()==SysRole.BLOCK_SEVEN){
            qw.in("draft_type",DraftType.SHORT, DraftType.MID, DraftType.LENGHT,DraftType.ACCOUNTS);
            qw.in("user_id", collect).eq("status_code", StateDic.BLOCK_SEVEN_PASS );
        }else if(selectRoleId()==SysRole.BLOCK_SIX) {
            qw.in("user_id", collect).eq("status_code", StateDic.BLOCK_SIX_PASS);
        }else {
            qw.in("user_id", collect).eq("status_code", StateDic.Block_Leader_Pass);//过了互查才到版块负责人,之前的他看不到
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            qw.lambda().between(Draft::getCreateTime,begin,end);
        }
        qw.lambda().orderByDesc(Draft::getUpdateTime);
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())) {
            qw.like("draft_title",draft.getDraftTitle());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftType())) {
            qw.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getIsPass())) {
            qw.eq("is_pass", draft.getIsPass());
        }

        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        return draftIPage;
    }


    @Override
    public IPage blockVideoIndex(Integer pageNo, Integer pageSize, Draft draft,String createTimeSpace) {
        SysUser currentUser = checkPartnerService.getCurrentUser();
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",currentUser.getId());
        List<Long> collect = relationMapper.selectList(queryWrapper).stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
        QueryWrapper<Draft> qw = new QueryWrapper<>();
//        qw.eq("Status_code",StateDic.Block_Leader_Pass);
        collect.add(-1L);
        log.info("aaaaaaaaaaaaaaaaaaaaa"+collect);
//        if(!ObjectUtils.isEmpty(collect)) {
        if(selectRoleId()==SysRole.BLOCK_SEVEN){
            qw.eq("draft_type",DraftType.SVIDEO);
            qw.in("user_id", collect).eq("status_code", StateDic.BLOCK_SEVEN_PASS);
        }else if(selectRoleId()==SysRole.BLOCK_SIX) {
            qw.in("user_id", collect).eq("status_code", StateDic.BLOCK_SIX_PASS);
        }else {
            qw.in("user_id", collect).eq("status_code", StateDic.Block_Leader_Pass);//过了互查才到版块负责人,之前的他看不到
//        }
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            qw.lambda().between(Draft::getCreateTime,begin,end);
        }
        qw.lambda().orderByDesc(Draft::getCreateTime);
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())) {
            qw.like("draft_title",draft.getDraftTitle());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftType())) {
            qw.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getIsPass())) {
            qw.eq("is_pass", draft.getIsPass());
        }

        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        return draftIPage;
    }

    /**
     * @Description 版块负责人YIBOHUI
     * @Author myr
     * @param pageNo
     * @param pageSize
     * @param draft
     * @return
     */
    @Override
    public IPage disallow(Integer pageNo, Integer pageSize, Draft draft,String createTimeSpace) {
        SysUser currentUser = checkPartnerService.getCurrentUser();
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",currentUser.getId());
        List<Long> collect = relationMapper.selectList(queryWrapper).stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
        QueryWrapper<Draft> qw = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(collect)) {
            qw.in("user_id", collect).eq("status_code", StateDic.Block_Leader_Reject);//过了互查才到版块负责人,之前的他看不到
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            qw.lambda().between(Draft::getCreateTime,begin,end);
        }
        qw.lambda().orderByDesc(Draft::getCreateTime);
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())) {
            qw.like("draft_title",draft.getDraftTitle());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftType())) {
            qw.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getIsPass())) {
            qw.eq("is_pass", draft.getIsPass());
        }

        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        return draftIPage;
    }
    /**
     * @Description 稿件校对人查询需要检查的稿件列表
     * @Author myr
     * @param pageNo
     * @param pageSize
     * @param draft
     * @return
     **/
    @Override
    public IPage draftCheckerIndex(Integer pageNo, Integer pageSize, Draft draft, String createTimeSpace) {
        QueryWrapper<Allot> qw = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(checkPartnerService.getCurrentUser().getId())) {
            qw.eq("allotNo_id", checkPartnerService.getCurrentUser().getId());
        }
        //实体List转化成某种类型的List
        List<Long> checkerDraft = allotMapper.selectList(qw).stream().map(Allot::getDraftId).collect(Collectors.toList());
        QueryWrapper<Draft> draftQueryWrapper = new QueryWrapper<>();

        if(!ObjectUtils.isEmpty(draft.getDraftType())) {
            draftQueryWrapper.eq("draft_type", draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getIsPass())) {
            draftQueryWrapper = draftQueryWrapper.eq("is_pass", draft.getIsPass());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())) {
            draftQueryWrapper = draftQueryWrapper.like("draft_title",draft.getDraftTitle());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftText())) {
            draftQueryWrapper = draftQueryWrapper.like("draft_text",draft.getDraftText());
        }
        if(!ObjectUtils.isEmpty(draft.getSubmitTime())) {

            draftQueryWrapper = draftQueryWrapper.like("submit_time",draft.getSubmitTime());
        }
        if(!ObjectUtils.isEmpty(draft.getGroupId())) {
            draftQueryWrapper = draftQueryWrapper.like("group_id",draft.getGroupId());
        }
        checkerDraft.add(-1L);
        if (draft.getStatusCode()!= null && draft.getStatusCode() == StateDic.refuse){
            draftQueryWrapper.in("id", checkerDraft).eq("status_code", StateDic.Draft_Final_Reviewer_Reject)
                    .or().eq("status_code",StateDic.Draft_Check_Leader_Reject);
        }else{
            draftQueryWrapper.in("id", checkerDraft).eq("status_code", draft.getStatusCode());
        }

        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            draftQueryWrapper.lambda().between(Draft::getCreateTime,begin,end);
//            if (!ObjectUtils.isEmpty(createTimeSpace)){
//                draftQueryWrapper.lambda().between(Draft::getCreateTime, DateUtil.getStart(createTimeSpace)
//                        ,DateUtil.getEnd(createTimeSpace).plusDays(1));
//            }
        }

        draftQueryWrapper.lambda().orderByDesc(Draft::getCreateTime);
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),draftQueryWrapper);
        log.info("checker Draft id is --> {} ",checkerDraft);
        log.info("checker draft entity is --> {}",draftIPage.getRecords().toString());
        return draftIPage;
    }

    /**
     * @Description 稿件校对组长审核首页
     * @param draft
     * @param pageNo
     * @param pageSize
     * @Author myr
     * @return
     **/
    @Override
    public IPage leaderCheckIndex(Draft draft, Integer pageNo, Integer pageSize) {
        log.info("抽查 draft --> {}",draft.toString());
//        List<SysUser> checkerList = relationService.allotBefore();//获取自己的组员
        QueryWrapper<Draft> qw = new QueryWrapper<>();
//        qw.in("group_id", checkerList.stream().map(SysUser::getId));//获取所有自己分配的稿件
        qw = qw.eq("group_id",draft.getGroupId());
        qw = qw.eq("status_code",StateDic.Draft_Checker_Pass);
        if(draft.getDraftType()!=null){
            qw = qw.eq("draft_type",draft.getDraftType());
        }
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        log.info("抽查 draftList --> {}",draftIPage.getRecords());
        return draftIPage;
    }

    /**
     * @Description 稿件终审人审核首页
     * @Author myr
     * @return
     **/
    @Override
    public IPage finalCheckIndex(Draft draft, Integer pageNo, Integer pageSize,String createTimeSpace) {
        QueryWrapper<Draft> qw = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            qw.lambda().between(Draft::getCreateTime,begin,end);
        }
        qw.lambda().orderByDesc(Draft::getCreateTime);
        if (draft.getGroupId() == null){
            draft.setGroupId("-1");
        }
        if(draft.getGroupId()!=null&&draft.getGroupId()!="") {
            qw.eq("group_id", draft.getGroupId());
        }
        if(draft.getDraftType()!=null) {
            qw.eq("draft_type", draft.getDraftType());
        }
        if(draft.getDraftTitle()!=null) {
            qw.like("draft_title", draft.getDraftTitle());
        }
        qw.eq("status_code",draft.getStatusCode());
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        return draftIPage;
    }


    /**
     * @Description //稿件终审人审核首页成功
     * @Author myr
     * @return
     **/
    @Override
    public IPage finalCheckIndexSuccess(Draft draft, Integer pageNo, Integer pageSize) {
        QueryWrapper<Draft> qw = new QueryWrapper<>();
//        if(draft.getGroupId()!=null&&draft.getGroupId()!="") {
//            qw.eq("group_id", draft.getGroupId());
//        }
        if(draft.getDraftTitle()!=null) {
            qw.like("draft_title", draft.getDraftTitle());
        }
        qw.lambda().orderByDesc(Draft::getCreateTime);
        qw.eq("status_code",StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_SUCCESS);
        if (draft.getDraftType() != null){
            qw.eq("draft_type",draft.getDraftType());
        }else {
            qw.eq("draft_type",StateDic.NULL);
        }
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        return draftIPage;
    }

    /**
     * @Description //稿件终审人审核首页失败
     * @Author myr
     * @return
     **/
    @Override
    public IPage finalCheckIndexFalse(Draft draft, Integer pageNo, Integer pageSize) {
        QueryWrapper<Draft> qw = new QueryWrapper<>();
//        if(draft.getGroupId()!=null&&draft.getGroupId()!="") {
//            qw.eq("group_id", draft.getGroupId());
//        }
        if(draft.getDraftTitle()!=null) {
            qw.like("draft_title", draft.getDraftTitle());
        }
        qw.lambda().orderByDesc(Draft::getUpdateTime);
        qw.eq("status_code",StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED).eq("is_pass",false);
        if (draft.getDraftType() != null){
            if(draft.getDraftType() == -1){
                qw.le("draft_type",DraftType.PPT);
            }else if(draft.getDraftType() == -6){
                qw.ge("draft_type",DraftType.VIDEO).le("draft_type",DraftType.WORD);
            }else if(draft.getDraftType() == -7){
                qw.ge("draft_type",DraftType.SHORT).le("draft_type",DraftType.ACCOUNTS);
            }else{
                qw.eq("draft_type",draft.getDraftType());
            }

        }

        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        return draftIPage;
    }
    /**
     * @Description 伙伴互查通过审核
     * @Author myr
     * @param draftId
     * @return
     */
    @Override
    public boolean partnerPass(Long draftId) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);
        Check check = new Check();//审核记录
        check.setPreStatusCode(draft.getStatusCode());
        check.setOverStatusCode(StateDic.Partner_Pass);
        check.setDraftId(draft.getId());
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Success);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        boolean checkNote = checkService.save(check);//稿件状态更新
        draft.setStatusCode(StateDic.Partner_Pass);
        draft.setIsPass(DraftIsPass.Pass);
        draft.setUpdateTime(LocalDateTime.now());
        draft.setLastOperator(loginUser);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description 互查伙伴驳回
     * @param draftId
     * @Author myr
     * @return
     **/
    @Override
    public boolean partnerDisallow(Long draftId, String disAllowdReason) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);
        Check check = new Check();//审核记录
        check.setPreStatusCode(draft.getStatusCode());
        check.setOverStatusCode(StatusWriter.STATUS_WRITER_PARTNER_FAILED);
        check.setDraftId(draftId);
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Fail);
        check.setReason(disAllowdReason);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        draft.setUpdateTime(LocalDateTime.now());//更新稿件状态
        draft.setIsPass(DraftIsPass.NotPass);
        draft.setLastOperator(loginUser);
        draft.setStatusCode(StatusWriter.STATUS_WRITER_PARTNER_FAILED);
        boolean checkNote = checkService.save(check);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description 版块负责人通过审核
     * @Author myr
     * @param draftId
     * @return
     */
    @Override
    public boolean blockLeaderPass(Long draftId) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);
        Check check = new Check();//审核记录
        check.setPreStatusCode(draft.getStatusCode());
        check.setOverStatusCode(StateDic.Block_Leader_Pass);
        check.setDraftId(draft.getId());
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Success);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        boolean checkNote = checkService.save(check);
        draft.setStatusCode(StateDic.Block_Leader_Pass);//稿件状态更新
        draft.setIsPass(DraftIsPass.Pass);
        draft.setUpdateTime(LocalDateTime.now());
        draft.setLastOperator(loginUser);
        log.info("block update --> {}",draft);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description 版块负责人驳回
     * @param draftId
     * @Author myr
     * @return
     **/
    @Override
    public boolean blockLeaderDisallow(Long draftId, String disAllowdReason) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);
        Check check = new Check();//审核记录
        check.setPreStatusCode(draft.getStatusCode());
        check.setOverStatusCode(StatusWriter.STATUS_WRRITE_SECTIONLEADER_FAILED);
        check.setDraftId(draftId);
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Fail);
        check.setReason(disAllowdReason);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        draft.setUpdateTime(LocalDateTime.now());//更新稿件状态
        draft.setIsPass(DraftIsPass.NotPass);
        draft.setLastOperator(loginUser);
        draft.setStatusCode(StatusWriter.STATUS_WRRITE_SECTIONLEADER_FAILED);
        boolean checkNote = checkService.save(check);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description 稿件校对人通过审核
     * @Author myr
     * @param draftId
     * @return
     */
    @Override
    public boolean checkerPass(Long draftId) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);
        Check check = new Check();//审核记录
        check.setPreStatusCode(draft.getStatusCode());
        check.setOverStatusCode(StateDic.Draft_Checker_Pass);
        check.setDraftId(draft.getId());
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Success);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        boolean checkNote = checkService.save(check);
        draft.setStatusCode(StateDic.Draft_Checker_Pass);//稿件状态更新
        draft.setIsPass(DraftIsPass.Pass);
        draft.setUpdateTime(LocalDateTime.now());
        draft.setLastOperator(loginUser);
        log.info("checker update --> {}",draft);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description 稿件校对人驳回
     * @param draftId
     * @Author myr
     * @return
     **/
    @Override
    public boolean checkerDisallow(Long draftId, String disAllowdReason) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);
        Check check = new Check();//审核记录
        check.setPreStatusCode(draft.getStatusCode());
        check.setOverStatusCode(StatusWriter.STATUS_WRRITE_ARTICLEREADER_FAILED);
        check.setDraftId(draftId);
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Fail);
        check.setReason(disAllowdReason);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        draft.setUpdateTime(LocalDateTime.now());//更新稿件状态
        draft.setIsPass(DraftIsPass.NotPass);
        draft.setLastOperator(loginUser);
        draft.setStatusCode(StatusWriter.STATUS_WRRITE_ARTICLEREADER_FAILED);
        boolean checkNote = checkService.save(check);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description 稿件校对组长抽查通过
     * @param draftGroup
     * @Author myr
     * @return
     **/
    @Override
    public boolean leaderCheckerPass(Long draftGroup) {
        //先验证这一组的是否都审核过了
        QueryWrapper<Draft> pro = new QueryWrapper<>();
        pro = pro.eq("group_id",draftGroup).ne("status_code",StateDic.Draft_Checker_Pass);
        List<Draft> proving = draftMapper.selectList(pro);
        if(proving.size()!=0){
            return false;
        }
        //验证通过走流程
        QueryWrapper<Draft> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("group_id",draftGroup).eq("status_code",StateDic.Draft_Checker_Pass);//必须等校对人审核过了才能批量提交
        List<Draft> draftList = draftMapper.selectList(queryWrapper);
        List<Check> checkList = new ArrayList<>();
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        for (int i = 0; i < draftList.size(); i++) {
            Check check = new Check();//审核记录
            check.setPreStatusCode(draftList.get(i).getStatusCode());
            check.setOverStatusCode(StateDic.Draft_Check_Leader_Pass);
            check.setDraftId(draftList.get(i).getId());
            check.setUserId(loginUser);
            check.setIsPass(DraftIsPass.Success);
            check.setCreateTime(LocalDateTime.now());
            check.setUpdateTime(LocalDateTime.now());
            checkList.add(check);
            draftList.get(i).setUpdateTime(LocalDateTime.now());
            draftList.get(i).setIsPass(DraftIsPass.Pass);
            draftList.get(i).setLastOperator(loginUser);
            draftList.get(i).setStatusCode(StateDic.Draft_Check_Leader_Pass);
        }
        boolean checkNote = checkService.saveBatch(checkList);
        boolean update = draftService.updateBatchById(draftList);
        return checkNote && update;
    }

    /**
     * @Description 稿件校对组长抽查驳回
     * @param draftId
     * @Author myr
     * @return
     **/
    @Override
    public boolean leaderCheckDisallow(Long draftId, String disAllowdReason) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);
        Check check = new Check();//审核记录
        check.setPreStatusCode(draft.getStatusCode());
        check.setOverStatusCode(StatusWriter.STATUS_WRRITE_ARTICLEREADERLEADER_FAILED);
        check.setDraftId(draftId);
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Fail);
        check.setReason(disAllowdReason);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        draft.setUpdateTime(LocalDateTime.now());//更新稿件状态
        draft.setIsPass(DraftIsPass.NotPass);
        draft.setLastOperator(loginUser);
        draft.setStatusCode(StatusWriter.STATUS_WRRITE_ARTICLEREADERLEADER_FAILED);
        boolean checkNote = checkService.save(check);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description 稿件终审人抽查通过
     * @param draftGroup
     * @Author myr
     * @return
     **/
    @Override
    public boolean finalCheckPass(String draftGroup) {
        //先验证这一组的是否都审核过了
        QueryWrapper<Draft> pro = new QueryWrapper<>();
        pro.eq("group_id",draftGroup).eq("status_code",StateDic.Draft_Check_Leader_Pass)
                .or().eq("group_id",draftGroup).eq("status_code",StateDic.SEVEN_LEADER_SUCCESS);
        List<Draft> proving = draftMapper.selectList(pro);
        if(ObjectUtils.isEmpty(proving)){
            return false;
        }
        //验证通过，开始走流程
        QueryWrapper<Draft> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id",draftGroup);
        queryWrapper.eq("status_code",StateDic.Draft_Check_Leader_Pass)//主流程组长已通过
                .or().eq("status_code",StateDic.SEVEN_LEADER_SUCCESS);//七队组长已通过

        List<Draft> draftList = draftMapper.selectList(queryWrapper);
        List<Check> checkList = new ArrayList<>();
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        if(!draftList.isEmpty()){
            for (int i = 0; i < draftList.size(); i++) {
                Check check = new Check();//审核记录
                check.setPreStatusCode(draftList.get(i).getStatusCode());
                check.setOverStatusCode(StateDic.Draft_Final_Reviewer_Pass);
                check.setDraftId(draftList.get(i).getId());
                check.setUserId(loginUser);
                check.setIsPass(DraftIsPass.Success);
                check.setCreateTime(LocalDateTime.now());
                check.setUpdateTime(LocalDateTime.now());
                checkList.add(check);
                draftList.get(i).setUpdateTime(LocalDateTime.now());
                draftList.get(i).setIsPass(DraftIsPass.Pass);
                draftList.get(i).setLastOperator(loginUser);
                draftList.get(i).setStatusCode(StateDic.Draft_Final_Reviewer_Pass);
            }
            boolean checkNote = checkService.saveBatch(checkList);
            boolean update = draftService.updateBatchById(draftList);
            return checkNote && update;
        }
        return false;
    }

    /**
     * @Description 稿件终审人抽查驳回
     * @param draftId
     * @Author myr
     * @return
     **/
    @Override
    public boolean finalCheckDisallow(Long draftId, String disAllowdReason) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);
        Check check = new Check();//审核记录
        check.setPreStatusCode(draft.getStatusCode());
        check.setOverStatusCode(StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED);
        check.setDraftId(draftId);
        check.setUserId(loginUser).setIsPass(DraftIsPass.Fail);
        check.setReason(disAllowdReason);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        draft.setUpdateTime(LocalDateTime.now());//更新稿件状态
        draft.setIsPass(DraftIsPass.NotPass);
        draft.setLastOperator(loginUser);
        draft.setStatusCode(StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED);
        boolean checkNote = checkService.save(check);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description 稿件审核驳回工具类 单个驳回
     * @param draftId
     * @Author myr
     * @return
     **/
    @Override
    public boolean disallowUtil(Long draftId, String disAllowdReason,Draft draft) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draftnew = draftMapper.selectById(draftId);//更改稿件状态
        Check check = new Check();//添加审核记录
        check.setPreStatusCode(draftnew.getStatusCode());
        check.setDraftId(draftId);
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Fail);
        check.setReason(disAllowdReason);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        draftnew.setUpdateTime(LocalDateTime.now());//更新稿件状态
        draftnew.setIsPass(DraftIsPass.NotPass);
        draftnew.setLastOperator(loginUser);
        draftnew.setDraftText(draft.getDraftText());
        draftnew.setDraftTitle(draft.getDraftTitle()).setFileName(draft.getFileName()).setDraftNum(draft.getDraftNum());
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();//获取登陆者角色id
        Integer code = -1;
        //根据角色来获取驳回状态
        if(rolesId == (SysRole.Draft_Final_Reviewer)){
            code = StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED;
        }else if(rolesId == (SysRole.Draft_Check_Leader)){
            code = StatusWriter.STATUS_WRRITE_ARTICLEREADERLEADER_FAILED;
        }else if(rolesId == (SysRole.Draft_Checker)){
            code = StatusWriter.STATUS_WRRITE_ARTICLEREADER_FAILED;//2005
        }else if(rolesId == (SysRole.Block_Leader)){
            code = StatusWriter.STATUS_WRRITE_SECTIONLEADER_FAILED;
        }else if(rolesId == (SysRole.Writer)){
            code = StatusWriter.STATUS_WRITER_PARTNER_FAILED;
        }else if(rolesId == SysRole.PARTTIME_SEVEN){
            code = StatusWriter.STATUS_WRITER_PARTNER_FAILED;
        }else if(rolesId == SysRole.PARTTIME_SIX){
            code = StatusWriter.STATUS_WRITER_PARTNER_FAILED;
        }else if(rolesId == SysRole.BLOCK_SEVEN){
            code = StatusWriter.STATUS_WRRITE_SECTIONLEADER_FAILED;
        }else if(rolesId == SysRole.BLOCK_SIX){
            code = StatusWriter.STATUS_WRRITE_SECTIONLEADER_FAILED;
        }else if(rolesId == SysRole.SEVEN_PROOFREADER){
            code = StatusWriter.STATUS_WRRITE_SEVEN_FAILED;
        }else if(rolesId == SysRole.SEVEN_LEADER){
            code = StatusWriter.STATUS_WRRITE_ARTICLEREADERLEADER_SEVEN_FAILED;
        }
        check.setOverStatusCode(code);
        draftnew.setStatusCode(code);
        //插入操作日志 审核拒绝
        applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_CHECK_NO)
                .setDraftId(draft.getId()).setReason(disAllowdReason));
        boolean checkNote = checkService.save(check);
        boolean update = draftService.updateById(draftnew);
        return checkNote && update;
    }

    /**
     * @Description 稿件审核通过工具类
     * @param draftId
     * @Author myr
     * @return
     **/
    @Override
    public boolean passUtil(Long draftId,Draft draftPre) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Draft draft = draftMapper.selectById(draftId);//更改稿件状态
        Check check = new Check();//添加审核记录
        log.info("draftId--->{}",draftId);
        log.info("draftPre--->{}",draftPre);
        if(draft.getStatusCode() != null){
            check.setPreStatusCode(draft.getStatusCode());
        }
        check.setDraftId(draftId);
        check.setUserId(loginUser);
        check.setIsPass(DraftIsPass.Success);
        check.setCreateTime(LocalDateTime.now());
        check.setUpdateTime(LocalDateTime.now());
        draft.setUpdateTime(LocalDateTime.now());//更新稿件状态
        draft.setIsPass(DraftIsPass.Pass);
        draft.setLastOperator(loginUser);
        if(draftPre.getDraftText()!=null) {
            draft.setDraftText(draftPre.getDraftText());
        }
        draft.setDraftTitle(draftPre.getDraftTitle()).setFileName(draftPre.getFileName()).setDraftNum(draftPre.getDraftNum());
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();//获取登陆者角色id
        Integer code = -1;
        if(rolesId == (SysRole.Draft_Final_Reviewer)){//根据角色来设置审核状态
            code = StateDic.Draft_Final_Reviewer_Pass;
        }else if(rolesId == (SysRole.Draft_Check_Leader)){
            code = StateDic.Draft_Check_Leader_Pass;
        }else if(rolesId == (SysRole.Draft_Checker)){
            code = StateDic.Draft_Checker_Pass;
        }else if(rolesId == (SysRole.Block_Leader)){
            if(ObjectUtils.isEmpty(draft.getGroupId())){//如果是被分配后打回的，则再审核的时候不需要再分配了
                code = StateDic.Block_Leader_Pass;
            }else {code = StateDic.Draft_Allocated;}
        }else if(rolesId == (SysRole.Writer)){
            if(draft.getStatusCode()==(StateDic.Partner_Reject))//谁打回，回哪去
                code = StateDic.Submit;
            else
                code = StateDic.Partner_Pass;
        }else if(rolesId == (SysRole.PARTTIME_SEVEN)){
            if(draft.getStatusCode()==(StateDic.Partner_Reject))//谁打回，回哪去
                code = StateDic.Submit;
            else
                code = StateDic.Partner_Pass;
        }else if(rolesId == (SysRole.PARTTIME_SIX)){
            if(draft.getStatusCode()==(StateDic.Partner_Reject))//谁打回，回哪去
                code = StateDic.Submit;
            else
                code = StateDic.Partner_Pass;
        }else if(rolesId == (SysRole.BLOCK_SEVEN)){
            if(ObjectUtils.isEmpty(draft.getGroupId())){//如果是被分配后打回的，则再审核的时候不需要再分配了
                code = StateDic.BLOCK_SEVEN_PASS;
            }else {code = StateDic.DRAFT_ALLOCATED_SEVEN;}
        } else if (rolesId == (SysRole.BLOCK_SIX)) {
            code = StateDic.BLOCK_SIX_PASS;
        }else if(rolesId == (SysRole.SEVEN_PROOFREADER)){
            code = StateDic.SEVEN_CHECKER_PASS;
        }else if(rolesId == (SysRole.SEVEN_LEADER)){
            code = StateDic.SEVEN_LEADER_SUCCESS;
        }

        check.setOverStatusCode(code);
        draft.setStatusCode(code);
        //插入操作日志 审核通过
        applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_CHECK_YES)
                .setDraftId(draft.getId()));
        boolean checkNote = checkService.save(check);
        boolean update = draftService.updateById(draft);
        return checkNote && update;
    }

    /**
     * @Description //稿件驳回列表首页
     * @Author myr
     * @return
     **/
    @Override
    public IPage disallowIndex(Draft draft, Integer pageNo, Integer pageSize,String createTimeSpace) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();//获取登陆者角色id
        log.info("登陆者角色 --> {}",rolesId);
        Integer code = -1;
        QueryWrapper<Draft> queryWrapper1 = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(draft.getDraftType())){
            queryWrapper1 = queryWrapper1.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())){
            queryWrapper1 = queryWrapper1.like("draft_title",draft.getDraftTitle());
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            queryWrapper1.lambda().between(Draft::getCreateTime,begin,end);
        }
        queryWrapper1.lambda().orderByDesc(Draft::getCreateTime);

        log.info("稿件列表 --> {}",rolesId==(SysRole.Draft_Check_Leader));
        if(rolesId.equals(SysRole.Draft_Final_Reviewer)){//根据角色来获取驳回稿件列表
            code = StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED;
        }else if(rolesId==(SysRole.Draft_Check_Leader)){//校对组长获取终审人驳回状态的
            code = StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED;
            List<SysUser> checkerList = relationService.allotBefore();//获取自己所有下属
            //拿到checkerList里面Id的List集合
            List<Long> checkerIds = checkerList.stream().map(SysUser::getId).collect(Collectors.toList());
            QueryWrapper<Allot> queryWrapper = new QueryWrapper<>();
            if(!ObjectUtils.isEmpty(checkerIds)) {
                queryWrapper.in("allotNo_id", checkerIds);
                List<Allot> allotList = allotMapper.selectList(queryWrapper);
                if(!ObjectUtils.isEmpty(allotList)) {
                    List<Long> draftIds = allotList.stream().map(Allot::getDraftId).collect(Collectors.toList());//获取所有下属审核的稿件
                    if (!ObjectUtils.isEmpty(code)) {
                        queryWrapper1 = queryWrapper1.in("id", draftIds).eq("status_code", code);//筛选出被驳回的
                    }
                }
            }
        }else if(rolesId==(SysRole.Draft_Checker)){//校对人获取校对组长驳回状态的
            code = StatusWriter.STATUS_WRRITE_ARTICLEREADERLEADER_FAILED;
            QueryWrapper<Allot> queryWrapper = new QueryWrapper<>();
            if(!ObjectUtils.isEmpty(loginUser)){
                queryWrapper.eq("allotNo_id",loginUser);
                List<Allot> allotList = allotMapper.selectList(queryWrapper);
                if(!ObjectUtils.isEmpty(allotList)) {
                    List<Long> draftIds = allotList.stream().map(Allot::getDraftId).collect(Collectors.toList());//获取校对人自己所有审核的稿件
                    if (!ObjectUtils.isEmpty(code)) {
                        queryWrapper1 = queryWrapper1.in("id", draftIds).eq("status_code", code);//筛选出被驳回的
                    }
                }
            }
        }else if(rolesId==(SysRole.Block_Leader)){//版块负责人获取驳回状态
            code = StatusWriter.STATUS_WRRITE_ARTICLEREADER_FAILED;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            if(!ObjectUtils.isEmpty(loginUser)) {
                queryWrapper.eq("parent_id", loginUser);
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                //所有下属写手的id列表
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());
//                writers.add(-1L);
                //判断板块负责人下属写手为空
                if(writers.size() != 0){
                    if (!ObjectUtils.isEmpty(code)) {
                        queryWrapper1 = queryWrapper1.in("user_id", writers)
                                .eq("status_code", code);//筛选出被驳回的
                    }
                }

            }
        }else if(rolesId==(SysRole.Writer) || rolesId==(SysRole.PARTTIME_SEVEN) || rolesId==(SysRole.PARTTIME_SIX)){//写手获取互查伙伴和版块负责人驳回状态的
            Integer code1 = StatusWriter.STATUS_WRITER_PARTNER_FAILED;
//            Integer code2 = StatusWriter.STATUS_WRRITE_SECTIONLEADER_FAILED;
            if(!ObjectUtils.isEmpty(loginUser)&&!ObjectUtils.isEmpty(code1)) {
                queryWrapper1 = queryWrapper1.eq("user_id", loginUser)
                        .and(wrapper -> wrapper.eq("status_code", code1));//筛选出被驳回的
            }
        }else if(rolesId==(SysRole.BLOCK_SEVEN)){//版块负责人获取驳回状态
            code = StatusWriter.STATUS_WRRITE_SEVEN_FAILED;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            if(!ObjectUtils.isEmpty(loginUser)) {
                queryWrapper.eq("parent_id", loginUser);
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                //所有下属写手的id列表
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());
//                writers.add(-1L);
                //判断板块负责人下属写手为空
                if(writers.size() != 0){
                    if (!ObjectUtils.isEmpty(code)) {
                        queryWrapper1 = queryWrapper1.in("user_id", writers)
                                .eq("status_code", code);//筛选出被驳回的
                    }
                }

            }
            queryWrapper1.in("draft_type",DraftType.SHORT, DraftType.MID, DraftType.LENGHT,DraftType.ACCOUNTS);
        }else if(rolesId==(SysRole.BLOCK_SIX)){//六队版块负责人获取驳回状态
            code = StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            if(!ObjectUtils.isEmpty(loginUser)) {
                queryWrapper.eq("parent_id", loginUser);
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                //所有下属写手的id列表
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());
//                writers.add(-1L);
                //判断板块负责人下属写手为空
                if(writers.size() != 0){
                    if (!ObjectUtils.isEmpty(code)) {
                        queryWrapper1 = queryWrapper1.in("user_id", writers)
                                .eq("status_code", code);//筛选出被驳回的
                    }
                }

            }
        }
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper1);
        return draftIPage;
    }

    @Override
    public IPage disallowVideoIndex(Draft draft, Integer pageNo, Integer pageSize,String createTimeSpace) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();//获取登陆者角色id
        log.info("登陆者角色 --> {}",rolesId);
        Integer code = -1;
        QueryWrapper<Draft> queryWrapper1 = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(draft.getDraftType())){
            queryWrapper1 = queryWrapper1.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())){
            queryWrapper1 = queryWrapper1.like("draft_title",draft.getDraftTitle());
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            queryWrapper1.lambda().between(Draft::getCreateTime,begin,end);
        }
        queryWrapper1.lambda().orderByDesc(Draft::getCreateTime);

        log.info("稿件列表 --> {}",rolesId==(SysRole.Draft_Check_Leader));
        if(rolesId.equals(SysRole.Draft_Final_Reviewer)){//根据角色来获取驳回稿件列表
            code = StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED;
        }else if(rolesId==(SysRole.Draft_Check_Leader)){//校对组长获取终审人驳回状态的
            code = StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED;
            List<SysUser> checkerList = relationService.allotBefore();//获取自己所有下属
            //拿到checkerList里面Id的List集合
            List<Long> checkerIds = checkerList.stream().map(SysUser::getId).collect(Collectors.toList());
            QueryWrapper<Allot> queryWrapper = new QueryWrapper<>();
            if(!ObjectUtils.isEmpty(checkerIds)) {
                queryWrapper.in("allotNo_id", checkerIds);
                List<Allot> allotList = allotMapper.selectList(queryWrapper);
                if(!ObjectUtils.isEmpty(allotList)) {
                    List<Long> draftIds = allotList.stream().map(Allot::getDraftId).collect(Collectors.toList());//获取所有下属审核的稿件
                    if (!ObjectUtils.isEmpty(code)) {
                        queryWrapper1 = queryWrapper1.in("id", draftIds).eq("status_code", code);//筛选出被驳回的
                    }
                }
            }
        }else if(rolesId==(SysRole.BLOCK_SEVEN)){//版块负责人获取驳回状态
            code = StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_FAILED;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            if(!ObjectUtils.isEmpty(loginUser)) {
                queryWrapper.eq("parent_id", loginUser);
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                //所有下属写手的id列表
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());
//                writers.add(-1L);
                //判断板块负责人下属写手为空
                if(writers.size() != 0){
                    if (!ObjectUtils.isEmpty(code)) {
                        queryWrapper1 = queryWrapper1.in("user_id", writers)
                                .eq("status_code", code);//筛选出被驳回的
                    }
                }

            }
            queryWrapper1.eq("draft_type",DraftType.SVIDEO);
        }
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper1);
        return draftIPage;
    }



    /**
     * @Description //稿件驳回列表首页
     * @Author myr
     * @return
     **/
    @Override
    public IPage disallow(Draft draft, Integer pageNo, Integer pageSize,String createTimeSpace) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();//获取登陆者角色id

        log.info("登陆者角色 --> {}",rolesId);
        Integer code = -1;
        QueryWrapper<Draft> queryWrapper1 = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(draft.getDraftType())){
            queryWrapper1 = queryWrapper1.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())){
            queryWrapper1 = queryWrapper1.like("draft_title",draft.getDraftTitle());
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            queryWrapper1.lambda().between(Draft::getCreateTime,begin,end);
        }
        queryWrapper1.eq("status_code",2003 ).eq("last_operator",loginUser);
        if(draftService.selectRoleId() == SysRole.BLOCK_SEVEN){
            queryWrapper1.in("draft_type",DraftType.SHORT, DraftType.MID, DraftType.LENGHT,DraftType.ACCOUNTS);
        }
        queryWrapper1.lambda().orderByDesc(Draft::getUpdateTime);
        log.info("稿件列表 --> {}",rolesId==(SysRole.Draft_Check_Leader));
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper1);
        return draftIPage;
    }


    @Override
    public IPage disallowVideo(Draft draft, Integer pageNo, Integer pageSize,String createTimeSpace) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();//获取登陆者角色id

        log.info("登陆者角色 --> {}",rolesId);
        Integer code = -1;
        QueryWrapper<Draft> queryWrapper1 = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(draft.getDraftType())){
            queryWrapper1 = queryWrapper1.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())){
            queryWrapper1 = queryWrapper1.like("draft_title",draft.getDraftTitle());
        }
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            queryWrapper1.lambda().between(Draft::getCreateTime,begin,end);
        }
        queryWrapper1.eq("status_code",2003 ).eq("last_operator",loginUser);
        if(draftService.selectRoleId() == SysRole.BLOCK_SEVEN){
            queryWrapper1.eq("draft_type",DraftType.SVIDEO);
        }
        queryWrapper1.lambda().orderByDesc(Draft::getCreateTime);
        log.info("稿件列表 --> {}",rolesId==(SysRole.Draft_Check_Leader));
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper1);
        return draftIPage;
    }


    /**
     * @Description //稿件待校对首页
     * @Author myr
     * @return
     **/
    @Override
    public IPage waitCheckIndex(Draft draft, Integer pageNo, Integer pageSize,String createTimeSpace) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();//获取登陆者角色id
        QueryWrapper<Draft> waitCheckDraft = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            waitCheckDraft.lambda().between(Draft::getCreateTime,begin,end);
        }
        if(!ObjectUtils.isEmpty(draft.getDraftType())){
            waitCheckDraft = waitCheckDraft.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())){
            waitCheckDraft = waitCheckDraft.like("draft_title",draft.getDraftTitle());
        }
        waitCheckDraft.lambda().orderByDesc(Draft::getCreateTime);
        Integer code = -1;
        log.info("登陆者角色 --> {}",rolesId);
        if(rolesId==(SysRole.Draft_Checker)){//校对人获取校对组长分配的
            code = StateDic.Draft_Allocated;
            QueryWrapper<Allot> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("allotNo_id",loginUser);
            List<Allot> allotList = allotMapper.selectList(queryWrapper);
            if(allotList != null) {
                List<Long> draftIds = allotList.stream().map(Allot::getDraftId).collect(Collectors.toList());//获取校对人自己所有审核的稿件
                if(!ObjectUtils.isEmpty(draftIds)) {
                    waitCheckDraft = waitCheckDraft.in("id", draftIds).eq("status_code", code);
                }
            }
        }else if(rolesId==(SysRole.Block_Leader)){//版块负责人获取互查伙伴审核通过的
            code = StateDic.Partner_Pass;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("parent_id",loginUser);
            if(queryWrapper !=null) {
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                    List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
                writers.add(-1L);
                if (!ObjectUtils.isEmpty(writers)) {
                    waitCheckDraft = waitCheckDraft.in("user_id", writers).eq("status_code", code);
            }
            }
        }else if(rolesId==(SysRole.Writer)){//写手获取互查伙伴提交的
            code = StateDic.Submit;
            QueryWrapper<CheckPartner> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("user_id",loginUser);
            if(queryWrapper != null) {
                Long partnerId = checkPartnerService.getOne(queryWrapper).getPartnerId();//找出互查伙伴id
                if (partnerId != null) {
                    waitCheckDraft = waitCheckDraft.eq("user_id", partnerId).eq("status_code", code);
                }
            }
        }else if(rolesId==(SysRole.BLOCK_SEVEN)){//版块负责人获取互查伙伴审核通过的
            code = StateDic.Partner_Pass;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("parent_id",loginUser);
            if(queryWrapper !=null) {
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
                writers.add(-1L);
                if (!ObjectUtils.isEmpty(writers)) {
                    waitCheckDraft = waitCheckDraft.in("user_id", writers).eq("status_code", code);
                }
            }
            waitCheckDraft.in("draft_type",DraftType.SHORT, DraftType.MID, DraftType.LENGHT,DraftType.ACCOUNTS);
        }else if(rolesId==(SysRole.BLOCK_SIX)){//6队版块负责人获取6队对接人审核通过的
            code = StateDic.Partner_Pass;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("parent_id",loginUser);
            if(queryWrapper !=null) {
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
                writers.add(-1L);
                if (!ObjectUtils.isEmpty(writers)) {
                    waitCheckDraft = waitCheckDraft.in("user_id", writers).eq("status_code", code);
                }
            }
        }else if (rolesId==(SysRole.Draft_Final_Reviewer)){
            code = StateDic.BLOCK_SIX_PASS;
                    waitCheckDraft = waitCheckDraft.eq("status_code", code);
                    log.info("waitCheckDraft-->{}",waitCheckDraft);

        }
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),waitCheckDraft);
        return draftIPage;
    }

    /**
     * @Description //稿件待校对首页
     * @Author myr
     * @return
     **/
    @Override
    public IPage waitCheckVideoIndex(Draft draft, Integer pageNo, Integer pageSize,String createTimeSpace) {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();//获取登陆者角色id
        QueryWrapper<Draft> waitCheckDraft = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            waitCheckDraft.lambda().between(Draft::getCreateTime,begin,end);
        }
        if(!ObjectUtils.isEmpty(draft.getDraftType())){
            waitCheckDraft = waitCheckDraft.eq("draft_type",draft.getDraftType());
        }
        if(!ObjectUtils.isEmpty(draft.getDraftTitle())){
            waitCheckDraft = waitCheckDraft.like("draft_title",draft.getDraftTitle());
        }
        waitCheckDraft.lambda().orderByDesc(Draft::getUpdateTime);
        Integer code = -1;
        log.info("登陆者角色 --> {}",rolesId);
        if(rolesId==(SysRole.Draft_Checker)){//校对人获取校对组长分配的
            code = StateDic.Draft_Allocated;
            QueryWrapper<Allot> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("allotNo_id",loginUser);
            List<Allot> allotList = allotMapper.selectList(queryWrapper);
            if(allotList != null) {
                List<Long> draftIds = allotList.stream().map(Allot::getDraftId).collect(Collectors.toList());//获取校对人自己所有审核的稿件
                if(!ObjectUtils.isEmpty(draftIds)) {
                    waitCheckDraft = waitCheckDraft.in("id", draftIds).eq("status_code", code);
                }
            }
        }else if(rolesId==(SysRole.Block_Leader)){//版块负责人获取互查伙伴审核通过的
            code = StateDic.Partner_Pass;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("parent_id",loginUser);
            if(queryWrapper !=null) {
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
                writers.add(-1L);
                if (!ObjectUtils.isEmpty(writers)) {
                    waitCheckDraft = waitCheckDraft.in("user_id", writers).eq("status_code", code);
                }
            }
        }else if(rolesId==(SysRole.Writer)){//写手获取互查伙伴提交的
            code = StateDic.Submit;
            QueryWrapper<CheckPartner> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("user_id",loginUser);
            if(queryWrapper != null) {
                Long partnerId = checkPartnerService.getOne(queryWrapper).getPartnerId();//找出互查伙伴id
                if (partnerId != null) {
                    waitCheckDraft = waitCheckDraft.eq("user_id", partnerId).eq("status_code", code);
                }
            }
        }else if(rolesId==(SysRole.BLOCK_SEVEN)){//版块负责人获取互查伙伴审核通过的
            code = StateDic.Partner_Pass;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("parent_id",loginUser);
            if(queryWrapper !=null) {
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
                writers.add(-1L);
                if (!ObjectUtils.isEmpty(writers)) {
                    waitCheckDraft = waitCheckDraft.in("user_id", writers).eq("status_code", code);
                }
            }
            waitCheckDraft.eq("draft_type",DraftType.SVIDEO);
        }else if(rolesId==(SysRole.BLOCK_SIX)){//6队版块负责人获取6队对接人审核通过的
            code = StateDic.Partner_Pass;
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            queryWrapper = queryWrapper.eq("parent_id",loginUser);
            if(queryWrapper !=null) {
                IPage<Relation> iPage = relationMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
                List<Long> writers = iPage.getRecords().stream().map(Relation::getUserId).collect(Collectors.toList());//所有下属写手的id列表
                writers.add(-1L);
                if (!ObjectUtils.isEmpty(writers)) {
                    waitCheckDraft = waitCheckDraft.in("user_id", writers).eq("status_code", code);
                }
            }
        }else if (rolesId==(SysRole.Draft_Final_Reviewer)){
            code = StateDic.BLOCK_SIX_PASS;
            waitCheckDraft = waitCheckDraft.eq("status_code", code);
            log.info("waitCheckDraft-->{}",waitCheckDraft);

        }
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),waitCheckDraft);
        return draftIPage;
    }


    /**
     * @Description 稿件类型数据回显
     * @Author myr
     * @return
     */
    @Override
    public Map setDraftType() {
        Map<Integer,String> types = new HashMap<Integer,String>();
        types.put(DraftType.Image_Text,"图文");
        types.put(DraftType.QA,"问答");
        types.put(DraftType.PPT,"PPT");
        types.put(DraftType.Audio,"音频");
        types.put(DraftType.VIDEO,"视频稿件");
        types.put(DraftType.WORD,"文字稿件");
        types.put(DraftType.SHORT,"短篇微博问答");
        types.put(DraftType.MID,"中篇微博问答");
        types.put(DraftType.LENGHT,"长篇微博问答");
        types.put(DraftType.ACCOUNTS,"公众号文章");
//        types.put(DraftType.SVIDEO,"视频稿件");
        return types;
    }

    /**
     * @Description 稿件审核状态回显
     * @Author myr
     * @return
     */
    @Override
    public Map setDraftCheckStatus() {
        Map<Integer,String> status = new HashMap<Integer,String>();
        status.put(DraftIsPass.Pass,"已通过");
        status.put(DraftIsPass.NotPass,"未通过");
        status.put(DraftIsPass.NotCheck,"未审核");
        return status;
    }

    /**
     * @Description 稿件分组回显
     * @Author myr
     * @return
     */
    @Override
    public Map setGroupStatus() {
        List<SysUser> checkerList = relationService.allotBefore();//获取自己的组员
        Map<String,String> status = new HashMap<>();
        for (int i = 0; i < checkerList.size(); i++) {
            status.put(String.valueOf(checkerList.get(i).getId()),checkerList.get(i).getUsername());
        }
        return status;
    }

    /**
     * @Description 稿件终审分组回显
     * @Author myr
     * @return
     */
    @Override
    public Map setFinalGroupStatus(Integer statusCode) {
        QueryWrapper<Draft> qw = new QueryWrapper<>();
       if (statusCode != null) {
           qw.eq("status_code", statusCode);
       }
        List<Draft> draftList = draftMapper.selectList(qw);
        List<String> collect = draftList.stream().map(Draft::getGroupId).collect(Collectors.toList());
        Map<String, String> status = new HashMap<>();
        List<String> userid = new ArrayList<>();
        for(int i = 0; i < collect.size(); i++){
            if(!userid.contains(collect.get(i))){
                userid.add(collect.get(i));
            }
        }
        if(!ObjectUtils.isEmpty(userid)) {
            if (userid.size() != 0) {
                for (int i = 0; i < userid.size(); i++) {
                    QueryWrapper<Group> wrapper = new QueryWrapper<>();
                    wrapper.eq("group_id",userid.get(i));
                    Group group = groupMapper.selectOne(wrapper);
                    if (group!=null){
                        status.put(userid.get(i), group.getName());
                    }

                }
            }
        }
        return status;
    }

    /**
     * @Description //状态码回显
     * @Author myr
     * @param
     * @return
     **/
    public Map setDraftStatus() {
        Map<Integer, String> status = new HashMap<Integer, String>();
        status.put(StateDic.Save, "已保存");
        status.put(StateDic.Submit, "已提交");
        status.put(StateDic.Partner_Pass, "待审核");
        status.put(StateDic.Block_Leader_Pass, "已审核");
        return status;
    }

    /**
     *@author 孙祺至
     *@date 2019/12/12 19:42
     */
    @Override
    public SysUser getUserIdByUserName(String username) {
        return userMapper.selectByUsername(username);
    }
/**
 *@author 孙祺至
 *@date 2019/12/12 19:42
 */
    @Override
    public List<Allot> getDraftByUserId(Long allotId) {
        return allotMapper.getDraftByUserId(allotId);
    }
/**
 *@author 孙祺至
 *@date 2019/12/12 19:42
 */
    @Override
    public List<Allot> getDraftByAllotnoId(Long allotNoId) {
        return allotMapper.getDraftByAllotnoId(allotNoId);
    }
/**
 *@author 孙祺至
 *@date 2019/12/12 19:42
 */
    @Override
    public List<Long> getChildrenIdByParentId(Long allotId) {
        return relationMapper.getChildrenIdByParentId(allotId);
    }
/**
 *@author 孙祺至
 *@date 2019/12/12 19:42
 */
    @Override
    public SysUser getUserNameByUserId(Long userid) {
        return userMapper.selectByPrimaryKey(userid);
    }
/**
 *@author 孙祺至
 *@date 2019/12/12 19:53
 */
    @Override
    public Integer insertToAllot(Allot allot) {
        return allotMapper.insert(allot);
    }

    @Override
    public Integer insertToGroup(Group group) {
        return groupMapper.insert(group);
    }

    /**
     * 刘雪奇
     */
    public SysUser getUserIdByUserNameAudio(String username) { return userMapper.selectByUsername(username); }

    /**
     * 刘雪奇
     */
    @Override
    public SysUser getUserNameAudioByUserId(Long userid) { return userMapper.selectByPrimaryKey(userid); }


    /**
     * @Author宋昱
     * @Description  根据name查找id
     * @Date 2019/12/10 17:34
     *
     * @return*/
    @Override
    public Long getUserIdByUsernameAudio(String username) {
        return userMapper.getUserIdByUsernameAudio(username);
    }

    /**
     * @Author宋昱
     * @Description 找到父类
     * @Date 2019/12/10 21:20
     **/
    @Override
    public List<Long> getChildrenNameByChildrenIdAudio(Long parentId) {
        return userMapper.getChildrenNameByChildrenIdAudio(parentId);
    }
    /**
     * @Author宋昱
     * @Description 根据userid找draftid
     * @Date 2019/12/16 10:30
     **/
    @Override
    public List<Long> selectDraftIdByAllot(Long allotNo_id) {
        return allotMapper.selectDraftIdByAllot(allotNo_id);
    }

    @Override
    public List<Long> selectDraftIdByAllotId(Long allot_id) {
        return allotMapper.selectDraftIdByAllotId(allot_id);
    }

    @Override
    public List<Draft> selectDraft(Long userid) {
        return draftMapper.selectDraft(userid);
    }

    @Override
    public int getNeedCheckSize(String groupId, int... statuscodes){
        return getNeedCheckSize(groupId,true,statuscodes);
    }

    /**
     * 同组 待校对稿件总数
     * @param groupId
     * @param inFlag
     * @param statuscodes
     * @return
     */
    @Override
    public int getNeedCheckSize(String groupId,boolean inFlag,int... statuscodes){
        QueryWrapper<Draft> queryWrapper = new QueryWrapper<Draft>();
        queryWrapper.lambda().eq(Draft::getGroupId,groupId);
        List<Integer> statusList = new ArrayList<Integer>();
        for (int statuscode : statuscodes) {
            statusList.add(statuscode);
        }
        if(inFlag){
            queryWrapper.lambda().in(Draft::getStatusCode,statusList);
        }else{
            queryWrapper.lambda().notIn(Draft::getStatusCode,statusList);
        }
        List<Draft> draftList = draftService.list(queryWrapper);
        return draftList.size();
    }

    @Override
    public int getNeedCheckSizePpt(String groupId,boolean inFlag,int... statuscodes){
        QueryWrapper<Ppt> queryWrapper = new QueryWrapper<Ppt>();
        queryWrapper.lambda().eq(Ppt::getGroupId,groupId);
        List<Integer> statusList = new ArrayList<Integer>();
        for (int statuscode : statuscodes) {
            statusList.add(statuscode);
        }
        if(inFlag){
            queryWrapper.lambda().in(Ppt::getStatusCode,statusList);
        }else{
            queryWrapper.lambda().notIn(Ppt::getStatusCode,statusList);
        }
        return pptService.list(queryWrapper).size();
    }
    @Override
    public int getNeedCheckSizeAudio(String groupId,boolean inFlag,int... statuscodes){
        QueryWrapper<Audio> queryWrapper = new QueryWrapper<Audio>();
        queryWrapper.lambda().eq(Audio::getGroupId,groupId);
        List<Integer> statusList = new ArrayList<Integer>();
        for (int statuscode : statuscodes) {
            statusList.add(statuscode);
        }
        if(inFlag){
            queryWrapper.lambda().in(Audio::getStatusCode,statusList);
        }else{
            queryWrapper.lambda().notIn(Audio::getStatusCode,statusList);
        }
        List<Audio> draftList = audioService.list(queryWrapper);
        return draftList.size();
    }
    @Override
    public int getNeedCheckSizeVideo(String groupId,boolean inFlag,int... statuscodes){
        QueryWrapper<Video> queryWrapper = new QueryWrapper<Video>();
        queryWrapper.lambda().eq(Video::getGroupId,groupId);
        List<Integer> statusList = new ArrayList<Integer>();
        for (int statuscode : statuscodes) {
            statusList.add(statuscode);
        }
        if(inFlag){
            queryWrapper.lambda().in(Video::getStatusCode,statusList);
        }else{
            queryWrapper.lambda().notIn(Video::getStatusCode,statusList);
        }
        List<Video> draftList = videoService.list(queryWrapper);
        return draftList.size();
    }
   /**
    * @Author 张雪
    * @Description //读取登录者role_id
    * @Date 2020/2/7 18:26
    **/
    @Override
    public long selectRoleId() {
        Long loginUser = checkPartnerService.getCurrentUser().getId();
        Long rolesId = sysUserRolesMapper.selectByUserId(loginUser).get(0).getRolesId();
        return rolesId;
    }
    /**
     * @Author 张雪
     * @Description //稿件终审人 批量通过 6队
     * @Date 2020/2/15 20:56
     * @Param
     * @return
     **/
    @Override
    public boolean checkSix(String ids) {
        if (ids != null){
            //ids转List
            String[] array = ids.split(",");
            Draft draft = new Draft();
                for (String id:array){
                    //更新稿件表
                    draft.setLastOperator(pptService.getUserByUserName(Utility.getCurrentUsername()).getId())
                            .setIsPass(1).setStatusCode(StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_SUCCESS)
                            .setUpdateTime(LocalDateTime.now())
                            .setId(Long.parseLong(id));
                    draftService.update(draft);
                }
            return true;
            }
        return false;
    }

    @Override
    public IPage sevenVideo(Draft draft, Integer pageNo, Integer pageSize,String createTimeSpace){
        QueryWrapper<Draft> qw = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(createTimeSpace)){
            String begin = createTimeSpace.substring(0,19);
            String end = createTimeSpace.substring(22);
            qw.lambda().between(Draft::getCreateTime,begin,end);
        }
        qw.lambda().orderByDesc(Draft::getCreateTime);
        if(draft.getDraftTitle()!=null) {
            qw.like("draft_title", draft.getDraftTitle());
        }
        if(draft.getStatusCode() != null){
            qw.eq("status_code",draft.getStatusCode());
        }
        qw.eq("draft_type",DraftType.SVIDEO);
        IPage<Draft> draftIPage = draftMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        return draftIPage;
    }

    @Override
    public String getTitleById(Long draftId) {
        return draftMapper.getTitleById(draftId);
    }

    @Override
    public String getFileNameById(Long draftId) {
        return draftMapper.getFileNameById(draftId);
    }

    @Override
    public Long getIdByUserName(String currentUsername) {
        return userMapper.getIdByUserName(currentUsername);
    }

    @Override
    public String getNameByUserId(Long allotnoId) {
        return userMapper.getNameByUserId(allotnoId);
    }

    @Override
    public IPage<Draft> listPage(Page<Draft> page, QueryWrapper<Draft> queryWrapper,Long idByUserName) {
        return page.setRecords(this.baseMapper.listPage(page,queryWrapper,idByUserName));
    }
    /**
     * @Author 张雪
     * @Description //稿件校对组长待抽查
     * @Date 2020/3/21 12:48
     * @Param
     * @return
     **/
    @Override
    public Map<Object,Object> draftCheckIndex(Page<Draft> page, Draft allot) {
        QueryWrapper<Draft> queryWrapper=new QueryWrapper<>();
        if(draftService.selectRoleId() == SysRole.SEVEN_LEADER){
            queryWrapper.eq("td.status_code", StatusWriter.STATUS_WRRITE_ARTICLEFINALLEADER_SEVEN_SUCCESS);
        }else {
            queryWrapper.eq("td.status_code", StatusWriter.STATUS_WRRITE_ARTICLEREADER_SUCCESS);
        }
        Long loginUserId = checkPartnerService.getCurrentUser().getId();
        List<Draft> drafts = draftMapper.selectPageList(page, loginUserId, queryWrapper);
        if(!ObjectUtils.isEmpty(allot.getGroupId())) {
            queryWrapper = queryWrapper.eq("td.group_id",allot.getGroupId());
        }
        Map<Object,Object> map = new HashMap();
        log.info("drafts-->{}",drafts);
        map.put("pageInfo",page.setRecords(draftMapper.selectPageList(page,loginUserId,queryWrapper)));
        map.put("drafts",drafts);
        return map;
    }


}
